package com.lagou.controller;

import com.lagou.common.entity.Result;
import com.lagou.common.entity.StatusCode;
import com.lagou.pojo.Recruit;
import com.lagou.service.RecruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/recruit")
public class RecruitController {
    @Autowired
    private RecruitService recruitService;

    /**
     * 查询所有数据
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询所有数据成功",recruitService.findAll());
    }

    /**
     * 根据id查询数据
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"根据id查询成功",recruitService.findById(id));
    }

    /**
     * 条件查询加分页
     */
    @RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
    public Result findsearch(@RequestBody Map whereMap, @PathVariable int page, @PathVariable int size){
        Page<Recruit> recruitList = recruitService.findSearch(whereMap, page, size);
        return new Result(true,StatusCode.OK,"条件查询加分页成功",recruitList);
    }

    /**
     * 条件查询
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result findsearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"调价查询成功",recruitService.findSearch(searchMap));
    }

    /**
     * 添加
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Recruit recruit){
        recruitService.add(recruit);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody Recruit recruit,@PathVariable String id){
        recruit.setId(id);
        recruitService.update(recruit);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        recruitService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 查询出推荐岗位，默认为9个
     */
    @RequestMapping(value = "/findTop9ByIsrcommend",method = RequestMethod.GET)
    public Result findTop9ByIsrcommend(){
        List<Recruit> recruitList = recruitService.findTop9ByIsrcommend();
        return new Result(true,StatusCode.OK,"查询推荐岗位成功",recruitList);
    }

    /**
     * 查询热门岗位，默认为9个
     */
    @RequestMapping(value = "/findTop9ByIshot",method = RequestMethod.GET)
    public Result findTop9ByIshot(){
        return new Result(true,StatusCode.OK,"查询热门岗位成功",recruitService.findTop9ByIshot());
    }

    /**
     * 查询出最新岗位 ，默认为9个
     */
    @RequestMapping(value = "/findTop9ByStateOrderByCreatetimeDesc",method = RequestMethod.GET)
    public Result findTop9ByStateOrderByCreatetimeDesc(){
        return new Result(true,StatusCode.OK,"查询最新岗位成功",recruitService.findTop9ByStateOrderByCreatetimeDesc());
    }

}
